package com.example.administrator.myapplication;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
    private BufferedReader bufferedReader;
        private String TAG = "MianActivityt";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();
    }

    private void initView() {

    }

    private void initData() {
        //内部存储
//        String fileName = "data.txt";
//        String content = "旷晓阳 201965110403";
//        FileOutputStream fileOutputStream = null;
//        try {
//            fileOutputStream = openFileOutput(fileName, MODE_PRIVATE);//用于打开应用程序中的输出流，将数据存储到指定的文件中
//            fileOutputStream.write(content.getBytes());
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            if (fileOutputStream != null) {
//                try {
//                    fileOutputStream.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }

        //外部存储
        String state = Environment.getExternalStorageState();
        Log.e(TAG, "initData: " + state);
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            File SDfile = Environment.getExternalStorageDirectory();//获取外部存储目录
            File file = new File(SDfile, "data.txt");
            String data = "旷晓阳 20196511043";
            FileOutputStream fileOutputStream1 = null;
            try {
                fileOutputStream1 = new FileOutputStream(file);
                fileOutputStream1.write(data.getBytes());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fileOutputStream1 != null) {
                    try {
                        fileOutputStream1.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        //读取内部存储中的文件数据
//        FileInputStream fileInputStream = null;
//        try {
//            fileInputStream = openFileInput("data.txt");
//            byte [] bytes = new byte[fileInputStream.available()];
//            fileInputStream.read(bytes);
//            String content = new String(bytes);
//            Toast.makeText(this, content+"", Toast.LENGTH_SHORT).show();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        //读取外部存储数据
        String state1 = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            File SDfile = Environment.getExternalStorageDirectory();
            File file = new File(SDfile, "data.txt");
            FileInputStream fileInputStream1 = null;
            try {
                fileInputStream1 = new FileInputStream(file);
                bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream1));
                String data = bufferedReader.readLine();
                Toast.makeText(this, data + "", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
                if(fileInputStream1 != null){
                    try {
                        fileInputStream1.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }

    }

    private void initEvent() {

    }
}
